package com.apartment.common.utils;

import com.apartment.common.utils.reflection.SerializableFunction;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public  class CollectionHelper<T> {

    /**
     * des. 根据集合中的一个属性分组
     *
     * @author : RazaJin
     * @date : 2020/9/9 0009 上午 11:27
     */
    public static Map<String, List<Map>> groupMapBy(List<Map> list, String groupBy) {

        Map<String, List<Map>> resultMap = new HashMap<>();
        for (Map ele : list) {
            if (resultMap.containsKey(ele.get(groupBy))) {
                resultMap.get(ele.get(groupBy)).add(ele);
            } else {
                List<Map> list2 = new ArrayList<>();
                list2.add(ele);
                resultMap.put(String.valueOf(ele.get(groupBy)), list2);
            }
        }
        return resultMap;
    }

    /**
     * @param list: 操作的集合
     * @param groupBy: 去重复条件  格式  "getXxx"  "get方法名"
     * @Description: list根据条件分组
     * @return: java.util.Map<java.lang.String, java.util.List < T>>
     * @Author: RazaJin
     * @Date: 2020/9/9 0009 上午 11:27
     **/
    public Map<String, List<T>> groupObjectBy(List<T> list, String groupBy) {
        return doGroupBy(list,groupBy);
    }

    public Map<String, List<T>> groupObjectBy(List<T> list, SerializableFunction<T,Object> serializableFunction) {
        String fieldName = ReflectionUtil.getGetterFieldName(serializableFunction);
        return doGroupBy(list,fieldName);
    }

    private Map<String,List<T>> doGroupBy(List<T> list,String groupBy){
        Map<String, List<T>> resultMap = new HashMap<>();
        if (list == null || list.size() == 0 || StringUtils.isEmpty(groupBy)) {
            return resultMap;
        }
        try {
            Class clazz = list.get(0).getClass();
            Method coinId = clazz.getMethod(groupBy);
            for (T ele : list) {
                if (resultMap.containsKey(coinId.invoke(ele))) {
                    resultMap.get(coinId.invoke(ele)).add(ele);
                } else {
                    List<T> list2 = new ArrayList<>();
                    list2.add(ele);
                    resultMap.put(String.valueOf(coinId.invoke(ele)), list2);
                }
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return resultMap;
    }




    /**
     * @param list:      集合
     * @param condition: 去重复条件  格式  "getXxx"  "get方法名"
     * @Description: 集合根据条件去重
     * @return: java.util.List<T>
     * @Author: @Dog_E
     * @Date: 2020/9/10 0010 16:30
     **/
    public List<T> deduplicationList(List<T> list, String condition) {
        try {
            Class clazz = list.get(0).getClass();
            Method coinId = clazz.getMethod(condition);
            //list 去重
            Map<String, T> hashMap = new HashMap<>();
            list.forEach(ele -> {
                try {
                    hashMap.put((coinId.invoke(ele)).toString(), ele);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            });
            list.clear();//清空集合
            hashMap.forEach((k, v) -> {
                list.add(v);
            });
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return list;
    }


}
